<?
    class Principio_Library_Template
    {
        private $templates;
        private $id;

        public function __construct($id_field, &$values)
        {
            $this->templates = array();
            $this->id = array();

            if (is_array($id_field))
            {
                foreach ($id_field as $id_field_name)
                    $this->id[$id_field_name] = null;
            }
            else
                $this->id[0] = null;


            if (is_array($values))
            {
                if (is_array($id_field))
                {
                    foreach ($id_field as $id_field_name)
                        $this->id[$id_field_name] = $values[0][$id_field_name];
                }
                else
                {
                    $this->id[0] = $values[0][$id_field];
                };

                foreach ($values as $value)
                    $this->templates[$value['ext_number']] = $value['template'];
            }
            elseif (is_integer($values))
            {
                $ext_count = intval($values);
                if (($ext_count > 0) && ($ext_count < 7))
                {
                    for ($i = 0; $i <= $ext_count; $i++)
                        $this->setTemplate($i, "");
                }
            };
        }

        public function getTemplate($template_index)
        {
            $template_index_ = intval($template_index);
            if (($template_index_ < 0) || ($template_index_ > $this->getExts() + 1))
                return false;

            return $this->templates[$template_index_];
        }

        public function setTemplate($template_index, $template)
        {
            $template_index_ = intval($template_index);
            if (($template_index_ < 0) || ($template_index_ > 6))
                return false;

            $this->templates[$template_index_] = $template;
            return true;
        }

        public function getExts()
        {
            return count($this->templates);
        }

        public function getID($id = null)
        {
            if (is_null($id))
                return $this->id[0];
            else
                return (!isset($this->id[$id]) ? false : $this->id[$id]);
        }

        public function setID($id)
        {
            if (is_array($id))
            {
                foreach ($id as $id_name => $value)
                {
                    $id_ = intval($value);
                    if ($id_ < 1)
                        return false;

                    $this->id[$id_name] = $id_;
                }
            }
            else
            {
                if (count($this->id) != 1)
                    return false;

                $id_ = intval($id);
                if ($id_ < 1)
                    return false;

                $this->id[0] = $id_;
            };
            return true;
        }
    };
?>